﻿Imports Training.Common

Partial Public Class EditGroupUser
    Inherits System.Web.UI.Page
    Public uName As String ' user name
    Dim uid As Integer

    ''' <summary>
    ''' Page Load event, check for querystring data and assign to variables
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' check for querystring data
        If IsNothing(Request.QueryString("uid")) Or IsNothing(Request.QueryString("unm")) Then
            Response.Redirect("Users.aspx")
        Else
            uid = Request.QueryString("uid")
            uName = Request.QueryString("unm")

            ' load user data into public variables
            GetUserInfo(uid)
            If Not Page.IsPostBack Then
                LoadVariables()
            Else
                ' check for check boxes to enable/disable
                If cbPassword.Checked Then
                    EnablePassword()
                Else
                    DisablePassword()
                End If

                If cbEmail.Checked Then
                    EnableEmail()
                Else
                    DisableEmail()
                End If
            End If
        End If
    End Sub

    ''' <summary>
    ''' Load data fields with variables from user information
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadVariables()
        tbFname.Text = firstname
        tbLname.Text = lastname
        tbAddress.Text = address
        tbCity.Text = city
        If stateid > 0 Then
            ddlState.SelectedValue = stateid
        End If
        tbZip.Text = zipcode
        tbPhone.Text = phone
        tbEmail.Text = email
        ddlGroup.SelectedValue = groupid
        tbSiteCode.Text = sitecode
        rblActive.SelectedValue = active

        lblLogin.Text = GetUserLogin(uid)
    End Sub

    ''' <summary>
    ''' Enable password fields and validators
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub EnablePassword()
        tbPass.Enabled = True
        rfPasword.Enabled = True
        tbPConf.Enabled = True
        rfPassConf.Enabled = True
        cvPass.Enabled = True
    End Sub

    ''' <summary>
    ''' Disable password fields and validators
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DisablePassword()
        tbPass.Enabled = False
        rfPasword.Enabled = False
        tbPConf.Enabled = False
        rfPassConf.Enabled = False
        cvPass.Enabled = False
    End Sub

    ''' <summary>
    ''' Enable email fields and validators
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub EnableEmail()
        tbLEmail.Enabled = True
        rfLEmail.Enabled = True
        reLEmail.Enabled = True
        tbEConf.Enabled = True
        rfEConf.Enabled = True
        cvEmail.Enabled = True
    End Sub

    ''' <summary>
    ''' Disable email fields and validators
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DisableEmail()
        tbLEmail.Enabled = False
        rfLEmail.Enabled = False
        reLEmail.Enabled = False
        tbEConf.Enabled = False
        rfEConf.Enabled = False
        cvEmail.Enabled = False
    End Sub

    ''' <summary>
    ''' Update User Info button click event. Updates User information
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Check for email change, if so also update email for membership</remarks>
    Private Sub btnUserInfo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUserInfo.Click
        If Page.IsValid Then
            If UpdateUserInfo(uid, tbFname.Text, tbLname.Text, ddlGroup.SelectedValue, tbAddress.Text, tbCity.Text, _
                           ddlState.SelectedValue, tbZip.Text, tbPhone.Text, tbEmail.Text, tbSiteCode.Text, rblActive.SelectedValue) > 0 Then
                lblInfoMsg.Text = "An Error occurred updating user information. Error has been logged."
            Else
                lblInfoMsg.Text = "User Information has been updated."
            End If

            ' check for change of email address to update membership email
            If Trim(tbEmail.Text) <> email Then
                Dim user As MembershipUser
                user = Membership.GetUser(GetUserLogin(uid))
                user.Email = Trim(tbEmail.Text)
                Membership.UpdateUser(user)
            End If
        Else
            lblInfoMsg.Text = "There was a problem validating form fields. Please try again."
        End If
    End Sub

    ''' <summary>
    ''' Button Login Info click event. Change email or password if checkboxes are enabled
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnLoginInfo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoginInfo.Click
        Dim user As MembershipUser
        user = Membership.GetUser(GetUserLogin(uid))

        ' if password checkbox is enabled
        If cbPassword.Checked Then
            ChangeMembershipPassword(GetUserLogin(uid), tbPass.Text)
        End If

        ' if email checkbox is enabled
        If cbEmail.Checked Then
            user.Email = Trim(tbLEmail.Text)
            Membership.UpdateUser(user)
        End If

        ' send email for password change, take OLD email unless its being changed as well
        If cbPassword.Checked Then
            If cbEmail.Checked Then
                ' send to new email address
                SendEmail(tbLEmail.Text, "Training Request Response", "Your request for a new password has been accepted. Your new password is: " & tbPass.Text)
            Else
                ' send to originall email address
                SendEmail(user.Email.ToString, "Training Request Response", "Your request for a new password has been accepted. Your new password is: " & tbPass.Text)
            End If
        End If

        ' alert user to changes
        lblLoginMsg.Text = "Login Information has been updated."
    End Sub
End Class